package cn.oesoft.tennissystem.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vdurmont.emoji.EmojiParser;

import cn.oesoft.tennissystem.dao.TennisRacketMapper;
import cn.oesoft.tennissystem.dao.CommentMapper;
import cn.oesoft.tennissystem.dao.StatisticMapper;
import cn.oesoft.tennissystem.model.entity.TennisRacket;
import cn.oesoft.tennissystem.model.entity.Statistic;
import cn.oesoft.tennissystem.service.ITennisRacketService;
import cn.oesoft.tennissystem.service.ITennisRacketService;

/**
 * @Classname TennisRacketServiceImpl
 * @Description TODO
 * @Date 2019-3-14 9:47
 * @Created by CrazyStone
 */
@Service
@Transactional
public class TennisRacketServiceImpl implements ITennisRacketService {
	//网球拍在redis缓存中的前缀
	private static final String REDIS_TENNIS_RACKET_PREFIX="tennisRacket_";
    @Autowired
    private TennisRacketMapper tennisRacketMapper;
    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CommentMapper commentMapper;

    // 分页查询网球拍列表
    @Override
    public PageInfo<TennisRacket> getWithPage(Integer page, Integer count) {
        PageHelper.startPage(page, count);
        List<TennisRacket> tennisRacketList = tennisRacketMapper.getWithPage();
        // 封装网球拍统计数据
        for (int i = 0; i < tennisRacketList.size(); i++) {
            TennisRacket tennisRacket = tennisRacketList.get(i);
            Statistic statistic = statisticMapper.getByTennisRacketId(tennisRacket.getId());
            tennisRacket.setHits(statistic.getHits());
            tennisRacket.setCommentsNum(statistic.getCommentsNum());
        }
        PageInfo<TennisRacket> pageInfo=new PageInfo<>(tennisRacketList);
        return pageInfo;
    }

    // 统计热度前10的网球拍信息
    @Override
    public List<TennisRacket> getHeatTennisRackets( ) {
        List<Statistic> list = statisticMapper.getStatistic();
        List<TennisRacket> TennisRacketlist=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            TennisRacket TennisRacket = tennisRacketMapper.getById(list.get(i).getTennisRacketId());
            TennisRacket.setHits(list.get(i).getHits());
            TennisRacket.setCommentsNum(list.get(i).getCommentsNum());
            TennisRacketlist.add(TennisRacket);
            if(i>=9){
                break;
            }
        }
        return TennisRacketlist;
    }

    // 根据id查询单个网球拍详情，并使用Redis进行缓存管理
    public TennisRacket getById(Integer id){
        TennisRacket TennisRacket = null;
        Object o = redisTemplate.opsForValue().get(REDIS_TENNIS_RACKET_PREFIX + id);
        if(o!=null){
            TennisRacket=(TennisRacket)o;
        }else{
            TennisRacket = tennisRacketMapper.getById(id);
            if(TennisRacket!=null){
                redisTemplate.opsForValue().set(REDIS_TENNIS_RACKET_PREFIX + id,TennisRacket);
            }
        }
        return TennisRacket;
    }

    // 发布网球拍
    @Override
    public void publish(TennisRacket tennisRacket) {
        // 去除表情
        tennisRacket.setContent(EmojiParser.parseToAliases(tennisRacket.getContent()));
        tennisRacket.setCreated(new Date());
        tennisRacket.setHits(0);
        tennisRacket.setCommentsNum(0);
        // 插入网球拍，同时插入网球拍统计数据
        tennisRacketMapper.insert(tennisRacket);
        statisticMapper.addStatistic(tennisRacket);
    }

    // 更新网球拍
    @Override
    public void updateById(TennisRacket tennisRacket) {
        tennisRacket.setModified(new Date());
        tennisRacketMapper.updateById(tennisRacket);
        redisTemplate.delete(REDIS_TENNIS_RACKET_PREFIX + tennisRacket.getId());
    }

    // 删除网球拍
    @Override
    public void deleteById(int id) {
        // 删除网球拍的同时，删除对应的缓存
        tennisRacketMapper.deleteById(id);
        redisTemplate.delete(REDIS_TENNIS_RACKET_PREFIX + id);
        // 同时删除对应网球拍的统计数据
        statisticMapper.deleteByTennisRacketId(id);
        // 同时删除对应网球拍的评论数据
        commentMapper.deleteCommentWithId(id);
    }
    //根据网球拍标题搜索
	@Override
	public List<TennisRacket> getByTitle(String title) {
		List<TennisRacket> list = tennisRacketMapper.getByTitle(title);
        for (TennisRacket tennisRacket : list) {
			Statistic statistic = statisticMapper.getByTennisRacketId(tennisRacket.getId());
			tennisRacket.setHits(statistic.getHits());
			tennisRacket.setCommentsNum(statistic.getCommentsNum());
		}
        return list;
	}

}

